//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include <libpub.h>
#include <bo_cmds.h>

#define KERNEL_RAM_START		0xA0020000

extern "C" {
int UsbDownloadImageEx();
int _UsbDownloadImageEx(unsigned long);
void BootKernel(int arg0, int arg1);
void wls_keypad_init();
char wls_keypad_getkey();

}

int Do_InitConfig(void)
{
    //TODO: Add your code here
    TOUT(1, 1, printf("Init Config\n"));
    return -1;
}

int Do_InitMemory(void)
{
    //Memory Init OK in Start.S
    TOUT(1, 1, printf("Init Memory\n"));
    return 0;
}

int Do_InitSerial(void)
{
    //Serial Init OK in start.S
    //SerialInit(baud115k2);
    //
    TOUT(1, 1, printf("Init Serial\n"));
    return 0;
}

int Do_InitKeypad(void)
{
    TOUT(1, 1, printf("Init Keypad\n"));
    wls_keypad_init();
    return 0;
}

DECL_INITCMD(Do_InitConfig, "Config");
DECL_INITCMD(Do_InitMemory, "Memory");
DECL_INITCMD(Do_InitSerial, "Serial");
DECL_INITCMD(Do_InitKeypad, "Keypad");

//==========================================================================
int Do_CheckKeyHold(void)
{
    int key = wls_keypad_getkey();
    TOUT(1, 1, printf("Get Key %x\n", key));
    if (key == 0x54) return 0;
    return key;//VK_DOWNLOAD;
}

int Do_Quit(char *param)
{
    TOUT(1, 1, printf("Quit [%s]\n", param));
    Bo_ExeCommand(0, "reset");
    //TODO: Add your code here
    return 0;
}

int Do_DownLoad(char *param)
{
    TOUT(1, 1, printf("DownLoad [%s]\n", param));
    unsigned long uImageBase = KERNEL_RAM_START;
    if (*param) param = Bo_ParseInteger(param, &uImageBase);

    char cmd[256];
    sprintf(cmd, "usb %08x", uImageBase);
    if (*param) Bo_ExeCommand(0, cmd);
    else {//old mode
        printk("Download with old mode!!!\n");
        UsbDownloadImageEx();
    }

    return 0;
}

int Do_Jump(char *param)
{
    TOUT(1, 1, printf("jump kernel [%s]\n", param));
    if (*param) {
        unsigned long address;
        param = Bo_ParseInteger(param, &address);
        unsigned long arg0 = 0, arg1 = 0;
        if (*param) {
            param = Bo_ParseInteger(param, &arg0);
            if (*param) {
                param = Bo_ParseInteger(param, &arg1);
            }
        }
        void (*Func)(int, int)= (void(*)(int, int))address;

        TOUT(1, 1, printf("jump 0x%08x(%x, %x)\n", Func, arg0, arg1));
        //(*Func)(arg0, arg1);
    }
    else {//old mode
        printk("Boot with old mode!!!\n");
        BootKernel(0, 0);
    }

	printf("Hey, the kernel returned! This should not happen.\n");

    return 0;
}



DECL_MAINCMD(Do_Quit, "quit {value}   - quit with return 'value'",  \
    "quit whit return value, default is 100");

DECL_MAINCMD(Do_DownLoad, \
    "download - download os image",  \
    "...\n");

DECL_MAINCMD(Do_Jump, \
    "jump kernel entrypoint",    \
    "...\n");

DECL_MAINCMD(Do_Reset, \
    "reset - cpu reset, then system will reboot.",    \
    "...\n");

DECL_MAINCMD(Do_Halt, "halt - cpu power off, then system shutdown.", "...\n");

//DECL_MAINCMD(Do_Upgrade, "Upgrade {}", "...\n");

DECL_MAINCMD(Do_ShowLogo, "logo", "...\n");
